package com.ziki.test.crypto;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class CryptoUtil {
    
    /**
     * DES,DESede,Blowfish
     * 
     * @param keybyte
     *            为加密密钥，长度为24字节
     * @param src
     *            为被加密的数据缓冲区（源）
     * @return
     */
    public static byte[] des3Eencrypt(byte[] keybyte, byte[] src) {
        try {
            String Algorithm = "DESede";
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            // 加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    
    /**
     * keybyte为加密密钥，长度为24字节
     * 
     * @param keybyte
     *            为加密密钥，长度为24字节
     * @param src
     *            为加密后的缓冲区
     * @return
     */
    public static byte[] des3Decrypt(byte[] keybyte, byte[] src) {
        try {
            String Algorithm = "DESede";
            // 生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            // 解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
    }
    
}
